{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "from diffusers import UNetSpatioTemporalConditionModel, StableVideoDiffusionPipeline\n",
    "from diffusers.utils import load_image, export_to_video, export_to_gif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "unet = UNetSpatioTemporalConditionModel.from_pretrained(\n",
    "    \"/path/to/unet\",\n",
    "    subfolder=\"unet\",\n",
    "    torch_dtype=torch.float16,\n",
    "    low_cpu_mem_usage=False,\n",
    ")\n",
    "pipe = StableVideoDiffusionPipeline.from_pretrained(\n",
    "    \"stabilityai/stable-video-diffusion-img2vid\",\n",
    "    unet=unet,\n",
    "    low_cpu_mem_usage=False,\n",
    "    torch_dtype=torch.float16, variant=\"fp16\", local_files_only=True,\n",
    ")\n",
    "pipe.to(\"cuda:0\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = load_image('dalle3_cat.jpg')\n",
    "image = image.resize((1024, 576))\n",
    "\n",
    "generator = torch.manual_seed(-1)\n",
    "with torch.inference_mode():\n",
    "    frames = pipe(image,\n",
    "                num_frames=14,\n",
    "                width=1024,\n",
    "                height=576,\n",
    "                decode_chunk_size=8, generator=generator, motion_bucket_id=127, fps=8, num_inference_steps=30).frames[0]\n",
    "export_to_video(frames, \"generated.mp4\", fps=7)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "svd",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
